﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKHO.Class;

namespace QLKHO.Forms
{
    public partial class frmBaoCaoXuatNhapTon : Form
    {
        public frmBaoCaoXuatNhapTon()
        {
            InitializeComponent();
        }

        private void butIn_Click(object sender, EventArgs e)
        {
            try 
            {
                DateTime dtime = dtpFrom.Value;
                bool flag=true;
                bool flagTinhTonDau = false;
                ADODB.Recordset Rs = new ADODB.Recordset();
                ADODB.Recordset RsNhapXuat = new ADODB.Recordset();
                ADODB.Recordset RsInsert = new ADODB.Recordset();
                string strSQL = "delete temptable_baocaoton " ;

                Rs.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                while (flag)
                {
                    foreach (DataRow rowPhieuNhap in qLKHODataSet.PHIEUNHAPKHO)
                    {
                        DateTime dtTemp = Convert.ToDateTime(rowPhieuNhap["NGAYLAPPHIEU"].ToString());
                        if (dtTemp.ToShortDateString() == dtime.ToShortDateString())
                        {
                            flagTinhTonDau = true;
                            break;
                        }
                    }
                    foreach (DataRow rowPhieuXuat in qLKHODataSet.PHIEUXUATKHO)
                    {
                        DateTime dtTemp=Convert.ToDateTime(rowPhieuXuat["NGAYLAPPHIEU"].ToString());
                        if (dtTemp.ToShortDateString() == dtime.ToShortDateString())
                        {
                            flagTinhTonDau = true;
                            break;
                        }
                    }
                    if (flagTinhTonDau == true)
                    {

                         strSQL = "proc_reportxuatnhapton '" + dtime.ToShortDateString() + "','" + dtime.ToShortDateString() + "'";
                        
                        Rs.Open(strSQL,clsMain.flexConn.fcConn , ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                        if (Rs.RecordCount > 0)
                        {
                            for (int i = 0; i < Rs.RecordCount; i++)
                            {
                                strSQL = "proc_inserttemptable 1,'" + dtime.ToShortDateString() + "',N'" + Rs.Fields[1].Value + "'," + Rs.Fields[2].Value + "," + Rs.Fields[3].Value + "," + Rs.Fields[4].Value + "," + Rs.Fields[5].Value;
                                RsInsert.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                                Rs.MoveNext();
                            }
                            
                        }
                        
                        Rs.Close();
                        
                    }
                    dtime=dtime.AddDays(1);
                    flagTinhTonDau = false;
                    if (Convert.ToDateTime(dtime.ToShortDateString()) >Convert.ToDateTime( dtpTo.Value.ToShortDateString()))
                        flag = false;
                }
                this.tEMPTABLE_BAOCAOTONTableAdapter.Fill(this.qLKHODataSet.TEMPTABLE_BAOCAOTON);
                this.reportViewer1.RefreshReport();
            }
            catch(Exception ex)
            {
                MessageBox.Show("butIn_Click\n"+ex.ToString());
            }
        }

        private void frmBaoCaoXuatNhapTon_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'qLKHODataSet.PHIEUXUATKHO' table. You can move, or remove it, as needed.
            this.pHIEUXUATKHOTableAdapter.Fill(this.qLKHODataSet.PHIEUXUATKHO);
            // TODO: This line of code loads data into the 'qLKHODataSet.PHIEUNHAPKHO' table. You can move, or remove it, as needed.
            this.pHIEUNHAPKHOTableAdapter.Fill(this.qLKHODataSet.PHIEUNHAPKHO);
            // TODO: This line of code loads data into the 'qLKHODataSet.TEMPTABLE_BAOCAOTON' table. You can move, or remove it, as needed.
            
        }

        
    }
}
